package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingMapper;
import com.sky.service.ShoppingService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingServiceImpl implements ShoppingService {
    @Autowired
    private ShoppingMapper shoppingMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addInShoppingCar(ShoppingCartDTO shoppingCartDTO) {
        //将其拷贝到
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //补全userid
        Long id= BaseContext.getCurrentId();
        //补全number
        shoppingCart.setNumber(1);
        shoppingCart.setUserId(id);
        //判断购物车是否已存在该商品
        //先查判断是否为空
        ShoppingCart s= shoppingMapper.select(shoppingCart);
        if(s!=null){//证明已经存在
            //只需要修改num的值加一
            shoppingCart.setNumber(shoppingMapper.select(shoppingCart).getNumber()+1);
            shoppingMapper.update(shoppingCart);
            return;
        }
        //若没有则添加
        shoppingCart.setCreateTime(LocalDateTime.now());
        //补全信息
        if(shoppingCart.getDishId()!=null){//该商品属于菜品
            DishVO dishVO = dishMapper.selectById(shoppingCart.getDishId());
            shoppingCart.setName(dishVO.getName());
            shoppingCart.setAmount(dishVO.getPrice());
            shoppingCart.setImage(dishVO.getImage());
        }
        else{//该商品属于套餐
            SetmealVO setmealVO = setmealMapper.getById(shoppingCart.getSetmealId());
            shoppingCart.setName(setmealVO.getName());
            shoppingCart.setImage(setmealVO.getImage());
            shoppingCart.setAmount(setmealVO.getPrice());
        }
        shoppingMapper.insert(shoppingCart);
    }

    @Override
    public List<ShoppingCart> checkShoppingCart() {
        return shoppingMapper.checkShoppingCart(BaseContext.getCurrentId());
    }

    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        //cop到
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //根据shoppingCartDTO查询信息
        ShoppingCart select = shoppingMapper.select(shoppingCart);
        //更改number数量
        Integer number= select.getNumber()-1;
        shoppingCart.setNumber(number);
        shoppingMapper.update(shoppingCart);
        if(number==0){
            shoppingMapper.delete(select);
        }
    }

    @Override
    public void clean() {
        Long userId=BaseContext.getCurrentId();
        shoppingMapper.clean(userId);
    }
}
